package gizmoball;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;


public class TriBrick extends Brick
{
	private int width = 20;
	public TriBrick(int x,int y)
	{
		super(x,y);
	}
	public TriBrick(int x,int y,int p,int d)
	{
		super(x,y,p,d);
	}
	
	public TriBrick(TriBrick b)
	{
		super(b);
		this.width = b.width;
	}
	
	public void setBrickx(int brickx) 
	{
		this.brickx = brickx;
	}
	
	public void setBricky(int bricky) 
	{
		this.bricky = bricky;
	}
	
	private int[] getpointx()
	{
		int[] pointx = new int[3];
		if(direction == 0)
		{
			pointx[0] = brickx-width/2;
			pointx[1] = brickx+width/2;
			pointx[2] = brickx+width/2;
		}
		else if(direction == 90)
		{
			pointx[0] = brickx-width/2;
			pointx[1] = brickx-width/2;
			pointx[2] = brickx+width/2;
		}
		else if(direction == 180)
		{
			pointx[0] = brickx-width/2;
			pointx[1] = brickx+width/2;
			pointx[2] = brickx-width/2;
		}
		else if(direction == 270)
		{
			pointx[0] = brickx+width/2;
			pointx[1] = brickx-width/2;
			pointx[2] = brickx+width/2;
		}
		return pointx;
	}
	
	private int[] getpointy()
	{
		int[] pointy = new int[3];
		if(direction == 0)
		{
			pointy[0] = bricky+width/2;
        	pointy[1] = bricky-width/2;
        	pointy[2] = bricky+width/2;
		}
		else if(direction == 90)
		{
			pointy[0] = bricky-width/2;
        	pointy[1] = bricky+width/2;       	
        	pointy[2] = bricky+width/2;
		}
		else if(direction == 180)
		{
			pointy[0] = bricky+width/2;        	
        	pointy[1] = bricky-width/2;       	
        	pointy[2] = bricky-width/2;
		}
		else if(direction == 270)
		{
			pointy[0] = bricky-width/2;       	
        	pointy[1] = bricky-width/2;        	
        	pointy[2] = bricky+width/2;
		}
		return pointy;
	}
	
	public void paint(Graphics g)
	{
    	// the "clip rectangle" is the area of the screen that needs to be
        // modified
        Rectangle clipRect = g.getClipBounds();
        Rectangle brick = new Rectangle(this.brickx,this.bricky,width,width);

        if (clipRect.intersects(brick)) 
        {
        	Color c1 = new Color(153,0,0); ; 
        	if(this.property == 1) 
        		c1 = new Color(108,123,139);
        	g.setColor(c1);
        	g.fillPolygon(getpointx(),getpointy(),3);
        	g.setColor(Color.black);
        	g.drawPolygon(getpointx(),getpointy(),3);
        }
	}
	
	public void rotation()
	{
		this.direction += 90;
		if(this.direction == 360)
		{
			this.direction = 0;
		}
		this.getpointx();
		this.getpointy();
	}
	
	public void large()
	{
		if(this.width<=40)
			this.width = this.width*2;
	}
	
	public void small()
	{
		if(this.width>=20)
			this.width = this.width/2;
	}
	
	public int judge(int x,int y,int radius)
	{
		if(this.direction == 0)
		{
			if(y<this.bricky+this.width/2 && y>this.bricky-this.width/2 
					&& x-radius<=this.brickx+this.width/2 && x-radius/2>=this.brickx+this.width/2)
			{
				this.bump = 0;
				return 90;
			}
			else if(x<this.brickx+this.width/2 && x>this.brickx-this.width/2
					&& y-radius<=this.bricky+this.width/2 && y-radius/2>=this.bricky+this.width/2)
			{
				this.bump = 0;
				return 180;
			}
			else if(y<this.bricky+this.width/2 && y>this.bricky-this.width/2
					&& x<this.brickx+this.width/2 && x>this.brickx-this.width/2
					&& Math.abs(x+radius/3-(this.brickx-this.width/2)) >= Math.abs(y+radius/3-(this.bricky+this.width/2)))
			{
				this.bump = 0;
				return 315;
			}
			else
				return -1;
		}
		else if(this.direction == 90)
		{
			if(y<this.bricky+this.width/2 && y>this.bricky-this.width/2 
					&& x+radius>=this.brickx-this.width/2 && x+radius/2<=this.brickx-this.width/2)
			{
				this.bump = 0;
				return 270;
			}
			else if(x<this.brickx+this.width/2 && x>this.brickx-this.width/2
					&& y-radius<=this.bricky+this.width/2 && y-radius/2>=this.bricky+this.width/2)
			{
				this.bump = 0;
				return 180;
			}
			else if(y<this.bricky+this.width/2 && y>this.bricky-this.width/2
					&& x<this.brickx+this.width/2 && x>this.brickx-this.width/2
					&& Math.abs(x-radius/3-(this.brickx-this.width/2)) <= Math.abs(y+radius/3-(this.bricky-this.width/2)))
			{
				this.bump = 0;
				return 45;
			}
			else
				return -1;
		}
		else if(this.direction == 180)
		{
			if(y<this.bricky+this.width/2 && y>this.bricky-this.width/2 
					&& x+radius>=this.brickx-this.width/2 && x+radius/2<=this.brickx-this.width/2)
			{
				this.bump = 0;
				return 270;
			}
			else if(x<this.brickx+this.width/2 && x>this.brickx-this.width/2
					&& y+radius>=this.bricky-this.width/2 && y+radius/2<=this.bricky-this.width/2)
			{
				this.bump = 0;
				return 0;
			}
			else if(y<this.bricky+this.width/2 && y>this.bricky-this.width/2
					&& x<this.brickx+this.width/2 && x>this.brickx-this.width/2
					&& Math.abs(x-radius/3-(this.brickx+this.width/2)) >= Math.abs(y-radius/3-(this.bricky-this.width/2)))
			{
				this.bump = 0;
				return 135;
			}
			else 
				return -1;
		}
		else
		{
			if(y<this.bricky+this.width/2 && y>this.bricky-this.width/2 
					&& x-radius<=this.brickx+this.width/2 && x-radius/2>=this.brickx+this.width/2)
			{
				this.bump = 0;
				return 90;
			}
			else if(x<this.brickx+this.width/2 && x>this.brickx-this.width/2
					&& y+radius>=this.bricky-this.width/2 && y+radius/2<=this.bricky-this.width/2)
			{
				this.bump = 0;
				return 0;
			}
			else if(y<this.bricky+this.width/2 && y>this.bricky-this.width/2
					&& x<this.brickx+this.width/2 && x>this.brickx-this.width/2
					&& Math.abs(x+radius/3-(this.brickx+this.width/2)) <= Math.abs(y-radius/3-(this.bricky+this.width/2)))
			{
				this.bump = 0;
				return 225;
			}
			else return -1;
		}
	}
	
	public int judge(int x,int y){
		if(x <= this.brickx + this.width/2 && x >= this.brickx - this.width/2
			&& y >= this.bricky - this.width/2 && y <= this.bricky + this.width/2){
			
			if(this.direction == 0){
				if(Math.abs(x - (this.brickx-this.width/2)) >= Math.abs(y - (this.bricky+this.width/2)))
					return -3;
				else
					return -1;
			}
			else if (this.direction == 90){
				if(Math.abs(x-(this.brickx-this.width/2)) <= Math.abs(y-(this.bricky-this.width/2)))
					return -3;
				else
					return -1;
			}
			else if (this.direction == 180){
				if(Math.abs(x-(this.brickx+this.width/2)) >= Math.abs(y-(this.bricky-this.width/2)))
					return -3;
				else
					return -1;
			}
			else if (this.direction == 270){
				if(Math.abs(x-(this.brickx+this.width/2)) <= Math.abs(y-(this.bricky+this.width/2)))
					return -3;
				else
					return -1;
			}
			else
				return -1;
		}
		else
			return -1;	
	}
	
}
